<template>
    <div class="login-container">
        <el-card class="login-card">
            <el-tabs v-model="activeTab">
                <el-tab-pane label="登录" name="login">
                    <el-form :model="loginForm" :rules="rules" ref="loginFormRef">
                        <el-form-item prop="username">
                            <el-input v-model="loginForm.username" placeholder="用户名">
                                <template #prefix>
                                    <el-icon>
                                        <User />
                                    </el-icon>
                                </template>
                            </el-input>
                        </el-form-item>
                        <el-form-item prop="password">
                            <el-input v-model="loginForm.password" type="password" placeholder="密码" show-password>
                                <template #prefix>
                                    <el-icon>
                                        <Lock />
                                    </el-icon>
                                </template>
                            </el-input>
                        </el-form-item>
                        <el-button type="primary" :loading="loading" @click="handleLogin"
                            style="width: 100%">登录</el-button>
                    </el-form>
                </el-tab-pane>
                <el-tab-pane label="注册" name="register">
                    <el-form :model="registerForm" :rules="rules" ref="registerFormRef">
                        <el-form-item prop="username">
                            <el-input v-model="registerForm.username" placeholder="用户名">
                                <template #prefix>
                                    <el-icon>
                                        <User />
                                    </el-icon>
                                </template>
                            </el-input>
                        </el-form-item>
                        <el-form-item prop="password">
                            <el-input v-model="registerForm.password" type="password" placeholder="密码" show-password>
                                <template #prefix>
                                    <el-icon>
                                        <Lock />
                                    </el-icon>
                                </template>
                            </el-input>
                        </el-form-item>
                        <el-button type="primary" :loading="loading" @click="handleRegister"
                            style="width: 100%">注册</el-button>
                    </el-form>
                </el-tab-pane>
            </el-tabs>
        </el-card>
    </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import { authApi } from '@/api'

const router = useRouter()
const activeTab = ref('login')
const loading = ref(false)
const loginFormRef = ref(null)
const registerFormRef = ref(null)

const loginForm = reactive({
    username: '',
    password: ''
})

const registerForm = reactive({
    username: '',
    password: ''
})

const rules = {
    username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
    ]
}

const handleLogin = async () => {
    if (!loginFormRef.value) return

    try {
        await loginFormRef.value.validate()
        loading.value = true

        const res = await authApi.login(loginForm)
        // 保存 token
        localStorage.setItem('token', res.token)
        ElMessage.success('登录成功')
        router.push('/dashboard')
    } catch (error) {
        console.error('登录失败:', error)
        ElMessage.error(error.message || '登录失败')
    } finally {
        loading.value = false
    }
}

const handleRegister = async () => {
    if (!registerFormRef.value) return

    try {
        await registerFormRef.value.validate()
        loading.value = true

        await authApi.register(registerForm)
        ElMessage.success('注册成功，请登录')
        activeTab.value = 'login'
        registerForm.username = ''
        registerForm.password = ''
    } catch (error) {
        console.error('注册失败:', error)
        ElMessage.error(error.message || '注册失败')
    } finally {
        loading.value = false
    }
}
</script>

<style scoped>
.login-container {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #f5f5f5;
}

.login-card {
    width: 400px;
}

:deep(.el-card__body) {
    padding: 30px;
}

:deep(.el-tabs__nav) {
    width: 100%;
    display: flex;
}

:deep(.el-tabs__item) {
    flex: 1;
    text-align: center;
}

:deep(.el-form-item:last-child) {
    margin-bottom: 0;
}

:deep(.el-button) {
    padding: 12px 20px;
}

:deep(.el-input__wrapper) {
    padding-left: 0;
}

:deep(.el-input__prefix) {
    padding: 0 8px;
}
</style>